package com.orangelife.webservice.service.propFeeComputeRule.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.bill.impl.BillService;
import com.orangelife.service.feeDetail.IFeeDetailService;
import com.orangelife.service.feeDetail.modal.PropFeeDetailDto;
import com.orangelife.service.propFeeComputeRule.IPropFeeComputeRuleService;
import com.orangelife.service.propFeeComputeRule.model.PropFeeComputeRuleDto;
import com.orangelife.service.propFeeComputeRuleInfo.IPropFeeComputeRuleInfoService;
import com.orangelife.service.propFeeRuleLinkApartment.IPropFeeRuleLinkApartmentService;
import com.orangelife.service.propFeeRuleLinkMeter.IPropFeeRuleLinkMeterService;
import com.orangelife.service.propFeeRuleParam.IPropFeeRuleParamService;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.base.BaseService;
import com.orangelife.webservice.base.PageUtils;
import com.orangelife.webservice.service.propFeeComputeRule.IPropFeeComputeRuleWebService;
import com.orangelife.webservice.service.propFeeComputeRule.entity.PropFeeComputeRule;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation 2015. All Rights Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * propFeeComputeRule类 的WebService基本作业
 * 
 * @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-25 10:41:10
 * ==============================================================
 */
@Service("webservice_propFeeComputeRule")
public class PropFeeComputeRuleWebServiceImpl extends BaseService implements
		IPropFeeComputeRuleWebService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name = "propFeeComputeRuleService")
	private IPropFeeComputeRuleService propFeeComputeRuleService;
	@Resource(name = "propFeeRuleLinkApartmentService")
	private IPropFeeRuleLinkApartmentService propFeeRuleLinkApartmentService;

	@Resource(name = "propFeeRuleParamService")
	private IPropFeeRuleParamService propFeeRuleParamService;

	@Resource(name = "propFeeRuleLinkMeterService")
	private IPropFeeRuleLinkMeterService propFeeRuleLinkMeterService;

	@Resource(name = "propFeeComputeRuleInfoService")
	private IPropFeeComputeRuleInfoService propFeeComputeRuleInfoService;

	@Resource(name = "feeDetailService")
	private IFeeDetailService feeDetailService;

	@Override
	public Response addPropFeeComputeRule(HttpServletRequest servletRequest,
			PropFeeComputeRuleDto propFeeComputeRule) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		propFeeComputeRule.setCreateuserID(pageUtils.getBaseSession()
				.getUserID());
		propFeeComputeRule.setInitID(IdUtil.getuuId());
		int result = propFeeComputeRuleService
				.addPropFeeComputeRule(propFeeComputeRule);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "添加实体成功");

		return this.normalResp();
	}

	@Override
	public Response editPropFeeComputeRule(HttpServletRequest servletRequest,
			PropFeeComputeRuleDto propFeeComputeRule) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(propFeeComputeRule.getRuleID(),
				"ruleID");
		if (error != null) {
			return error;
		}

		/************************************************
		 * 输入参数检查
		 ************************************************/
		// TODO;

		propFeeComputeRule
				.setEdituserID(pageUtils.getBaseSession().getUserID());
		int result = propFeeComputeRuleService
				.editPropFeeComputeRule(propFeeComputeRule);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "修改实体成功");

		return this.normalResp();
	}

	@Override
	public Response deletePropFeeComputeRule(HttpServletRequest servletRequest,
			String ruleID) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(ruleID, "ruleID");
		if (error != null) {
			return error;
		}
		PropFeeComputeRuleDto oldPropFeeComputeRule = propFeeComputeRuleService
				.findPropFeeComputeRuleByID(ruleID);
		if (oldPropFeeComputeRule == null) {
			return this.abnormalResp(99, "亲，该规则已经被删除");
		}
		if (BillService.getFeeStatus(oldPropFeeComputeRule.getCommID())) {
			return this.abnormalResp(98, "亲，该小区正在生成缴费账单，请稍后操作");
		}
		String user = (StringUtils.isBlank(pageUtils.getBaseSession()
				.getRealname()) ? pageUtils.getBaseSession().getLoginName()
				: pageUtils.getBaseSession().getRealname());
		int result = 0;
		List<PropFeeDetailDto> propFeeDetails = feeDetailService
				.findPropFeeDetailByRelevantID(null, null, null, null, null,
						null, null, ruleID, null);

		if (propFeeDetails != null && propFeeDetails.size() > 0) {
			result = propFeeComputeRuleInfoService
					.deletePropFeeComputeRuleByLogic(ruleID, user, pageUtils
							.getBaseSession().getUserID());
		} else {
			result = propFeeComputeRuleService.deletePropFeeComputeRuleByLogic(
					ruleID, pageUtils.getBaseSession().getUserID());
		}

		// int result =
		// propFeeComputeRuleService.deletePropFeeComputeRuleByLogic(
		// ruleID, pageUtils.getBaseSession().getUserID());
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "删除实体成功");

		return this.normalResp();
	}

	@Override
	public Response findPropFeeComputeRuleByID(
			HttpServletRequest servletRequest, String ruleID) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(ruleID, "ruleID");
		if (error != null) {
			return error;
		}
		PropFeeComputeRuleDto propFeeComputeRule = propFeeComputeRuleService
				.findPropFeeComputeRuleByID(ruleID);

		propFeeComputeRule
				.setPropFeeRuleLinkApartments(propFeeRuleLinkApartmentService
						.findPropFeeRuleLinkApartmentByRelevantID(null, ruleID,
								null, null));
		propFeeComputeRule
				.setPropFeeRuleLinkMeters(propFeeRuleLinkMeterService
						.findPropFeeRuleLinkMeterByRelevantID(null, ruleID,
								null, null));
		propFeeComputeRule.setPropFeeRuleParams(propFeeRuleParamService
				.findPropFeeRuleParamByRelevantID(ruleID, null, null));
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "查询实体成功");

		return this.normalResp2(propFeeComputeRule);
	}

	public IPropFeeComputeRuleService getPropFeeComputeRuleService() {
		return propFeeComputeRuleService;
	}

	public void setPropFeeComputeRuleService(
			IPropFeeComputeRuleService propFeeComputeRuleService) {
		this.propFeeComputeRuleService = propFeeComputeRuleService;
	}

	public IPropFeeRuleLinkApartmentService getPropFeeRuleLinkApartmentService() {
		return propFeeRuleLinkApartmentService;
	}

	public void setPropFeeRuleLinkApartmentService(
			IPropFeeRuleLinkApartmentService propFeeRuleLinkApartmentService) {
		this.propFeeRuleLinkApartmentService = propFeeRuleLinkApartmentService;
	}

	public IPropFeeRuleParamService getPropFeeRuleParamService() {
		return propFeeRuleParamService;
	}

	public void setPropFeeRuleParamService(
			IPropFeeRuleParamService propFeeRuleParamService) {
		this.propFeeRuleParamService = propFeeRuleParamService;
	}

	public IPropFeeRuleLinkMeterService getPropFeeRuleLinkMeterService() {
		return propFeeRuleLinkMeterService;
	}

	public void setPropFeeRuleLinkMeterService(
			IPropFeeRuleLinkMeterService propFeeRuleLinkMeterService) {
		this.propFeeRuleLinkMeterService = propFeeRuleLinkMeterService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Override
	public Response getAllPropFeeComputeRule(HttpServletRequest servletRequest,
			String type, String commID, String search, String tsCreate,
			String pageIndex, String pageSize) throws Exception {

		PageUtils pageUtils = this.validatePageAndSetPage(servletRequest,
				pageIndex, pageSize);
		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (!StringUtils.isBlank(tsCreate)) {
			StringUtils.isTime(tsCreate);
			tsCreate = String.valueOf(Long.parseLong(tsCreate) / 1000);
		}
		/************************************************
		 * 查询列表
		 ************************************************/
		List<Map<String, Object>> list = propFeeComputeRuleService
				.findPropFeeComputeRuleByPage(type, commID,
						tsCreate == null ? null : tsCreate,
						search == null ? null : search, pageUtils
								.getiPageIndex(), pageUtils.getiPageSize());

		/************************************************
		 * 返回结果设定
		 ************************************************/
		Long total = ((Page<Map<String, Object>>) list).getTotal();

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginName() + new Date()
				+ "查询实体成功");

		return this.normalResp(pageUtils.getiPageIndex(),
				pageUtils.getiPageSize(), total,
				PropertyUtils.turnToEntity(list));
	}

	@Override
	public Response bauthFindPropFeeComputeRule(
			HttpServletRequest servletRequest, List<String> propFeeComputeRule)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeComputeRule == null || propFeeComputeRule.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity", propFeeComputeRuleService
					.bauthfindPropFeeComputeRule(propFeeComputeRule));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthFindPropFeeComputeRules(
			HttpServletRequest servletRequest,
			Map<String, Object> propFeeComputeRule) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeComputeRule == null || propFeeComputeRule.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity",
					PropertyUtils.turnToEntity(propFeeComputeRuleService
							.bauthfindPropFeeComputeRuleByMap(propFeeComputeRule)));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response clearCache(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		propFeeComputeRuleService.clearCache();
		return this.normalResp();
	}

	@Override
	public Response addPropFeeComputeRules(HttpServletRequest servletRequest,
			PropFeeComputeRule propFeeComputeRule) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		propFeeComputeRule.setCreateuserID(pageUtils.getBaseSession()
				.getUserID());
		int result = propFeeComputeRuleService
				.addFeeComputeRule(propFeeComputeRule);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "添加实体成功");

		return this.normalResp();
	}

	@Override
	public Response editPropFeeComputeRules(HttpServletRequest servletRequest,
			PropFeeComputeRule propFeeComputeRule) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		propFeeComputeRule.setCreateuserID(pageUtils.getBaseSession()
				.getUserID());

		propFeeComputeRule.setCreateUser(StringUtils.isBlank(pageUtils
				.getBaseSession().getRealname()) ? pageUtils.getBaseSession()
				.getLoginName() : pageUtils.getBaseSession().getRealname());

		if (StringUtils.isBlank(propFeeComputeRule.getCommID())) {
			return this.abnormalResp(2, "亲，小区信息");
		}
		if (BillService.getFeeStatus(propFeeComputeRule.getCommID())) {
			return this.abnormalResp(98, "亲，该小区正在生成缴费账单，请稍后操作");
		}

		List<PropFeeDetailDto> propFeeDetails = feeDetailService
				.findPropFeeDetailByRelevantID(null, null, null, null, null,
						null, null, propFeeComputeRule.getRuleID(), null);
		int result = 0;
		if (propFeeDetails != null && propFeeDetails.size() > 0) {
			result = propFeeComputeRuleInfoService
					.editFeeComputeRule(propFeeComputeRule);
		} else {
			result = propFeeComputeRuleService
					.editFeeComputeRule(propFeeComputeRule);
		}

		// int result = propFeeComputeRuleService
		// .editFeeComputeRule(propFeeComputeRule);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "添加实体成功");

		return this.normalResp();
	}

	@Override
	public Response addPoolTypePropFeeComputeRules(
			HttpServletRequest servletRequest,
			PropFeeComputeRule propFeeComputeRule) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		propFeeComputeRule.setCreateuserID(pageUtils.getBaseSession()
				.getUserID());
		int result = propFeeComputeRuleService
				.addPoolTypeFeeComputeRule(propFeeComputeRule);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "添加实体成功");

		return this.normalResp();
	}

	@Override
	public Response editPoolTypePropFeeComputeRules(
			HttpServletRequest servletRequest,
			PropFeeComputeRule propFeeComputeRule) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (StringUtils.isBlank(propFeeComputeRule.getCommID())) {
			return this.abnormalResp(2, "亲，小区信息");
		}
		if (BillService.getFeeStatus(propFeeComputeRule.getCommID())) {
			return this.abnormalResp(98, "亲，该小区正在生成缴费账单，请稍后操作");
		}

		propFeeComputeRule.setCreateuserID(pageUtils.getBaseSession()
				.getUserID());
		propFeeComputeRule.setCreateUser(StringUtils.isBlank(pageUtils
				.getBaseSession().getRealname()) ? pageUtils.getBaseSession()
				.getLoginName() : pageUtils.getBaseSession().getRealname());
		int result = 0;

		List<PropFeeDetailDto> propFeeDetails = feeDetailService
				.findPropFeeDetailByRelevantID(null, null, null, null, null,
						null, null, propFeeComputeRule.getRuleID(), null);

		if (propFeeDetails != null && propFeeDetails.size() > 0) {
			result = propFeeComputeRuleInfoService
					.editPoolTypeFeeComputeRule(propFeeComputeRule);
		} else {
			result = propFeeComputeRuleService
					.editPoolTypeFeeComputeRule(propFeeComputeRule);
		}

		// int result = propFeeComputeRuleService
		// .editPoolTypeFeeComputeRule(propFeeComputeRule);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "添加实体成功");

		return this.normalResp();
	}

	@Override
	public Response getOtherfee(HttpServletRequest request,
			Map<String, Object> paMap) throws Exception {


		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(request);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		Map<String, Object> map = propFeeComputeRuleService
				.getOtherMoney(paMap);
		return this.normalResp(map);
	}

	@Override
	public Response getFeeComputeRuleType(HttpServletRequest servletRequest,
			Map<String, Object> propFeeComputeRule) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeComputeRule == null || propFeeComputeRule.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity", PropertyUtils
					.turnToEntity(propFeeComputeRuleService
							.getFeeComputeRuleType(propFeeComputeRule)));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}
}
